// ignore_for_file: prefer_const_constructors, invalid_use_of_visible_for_testing_member
 
import 'dart:typed_data';

import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

Future<void> requestCameraPermission() async {
  final status = await Permission.camera.request();
  if (status.isGranted) {
    // 权限被授予
  } else {
    // 权限被拒绝
  }
}


class QGSelectAvatar extends StatefulWidget {
  const QGSelectAvatar({super.key});

  @override
  State<QGSelectAvatar> createState() => _QGSelectAvatarState();
}

class _QGSelectAvatarState extends State<QGSelectAvatar> {
  Uint8List? imageAsUint8List;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 198.rpx,
      child: Column(
        children: [
          buildCameraInfo(),
          SizedBox(
            height: 8.rpx,
          ),
          buildCancel()
        ],
      ),
    );
  }

  buildCameraInfo() {
    return Container(
      margin: EdgeInsets.only(left: 10.rpx, right: 10.rpx),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.rpx),
          color: Color(0xFF232A32)),
      child: Column(
        children: [buildCamera(), buildAlbum()],
      ),
    );
  }

  buildCamera() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Expanded(
            child: GestureDetector(
                onTap: () {
                  chooseImage(ImageSource.camera);
                },
                child: Container(
                  alignment: Alignment.center,
                  height: 52.rpx,
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              
                              ))),
                  child:
                      buildText('拍照', 16, FontWeight.normal, Color(0xFFffffff)),
                )))
      ],
    );
  }

  buildAlbum() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Expanded(
            child: InkWell(
                onTap: () async{
                    chooseImage(ImageSource.gallery);
                },
                child: Container(
                  alignment: Alignment.center,
                  height: 52.rpx,
                  child: buildText(
                      '从相册选择', 16, FontWeight.normal, Color(0xFFffffff)),
                )))
      ],
    );
  }

  Future chooseImage(ImageSource source) async {

   final  del = await Future.delayed(const Duration(milliseconds: 300));

  try {
      final pickerImages = await ImagePicker().pickImage(source: source);
  if (pickerImages != null) {
      imageAsUint8List = await pickerImages.readAsBytes();

      

      Navigator.of(context).pushReplacementNamed(pageMineClipAvatar,
          arguments: imageAsUint8List);
    } else {
      imageAsUint8List = null;
    }
  } catch (e) {
    debugPrint('Picker error: $e');
   
}
 

    

    // if (mounted) {
    //   setState(() {
    //     Navigator.of(context).pop();
    //     if (pickerImages != null) {
    //       Navigator.of(context).pushNamed(pageMineClipAvatar,
    //           arguments: File(pickerImages.path));
    //     } else {
    //       print('没有照片可以选择');
    //     }
    //   });
    // }
  }

  buildCancel() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(width: 10.rpx),
        Expanded(
          child: GestureDetector(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Container(
              alignment: Alignment.center,
              height: 52.rpx,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10.rpx),
                  color: Color(0xFF232A32)),
              child: buildText('取消', 16, FontWeight.normal, Color(0xFFFFFFFF)),
            ),
          ),
        ),
        SizedBox(width: 10.rpx),
      ],
    );
  }

  buildText(String content, int size, FontWeight weight, Color color) {
    return Text(
      content,
      style: TextStyle(fontSize: size.rpx, fontWeight: weight, color: color),
    );
  }
}
